<!-- 新增电子签名 -->
<template>
  <div id="dialog-container">
    <el-dialog v-dialogDrag :title="'修改电子签名'" append-to-body :close-on-click-modal="false"
      :visible.sync="dialogVisible" @close="$emit('update:show', false)" :before-close="handleClose" :show="show" width="680px">
      <el-form :model="formData" :inline="true" label-width="90px" size="small" ref="form">
        <el-row>
          <el-col :span="12">
           <el-form-item label="工号：">
              <el-input v-model="formData.userName"disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
             <el-form-item label="用户中文名：">
                <el-input v-model="formData.name" disabled style="width: 150px;"></el-input>
            </el-form-item>
            
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="电子签名：">
              <el-upload
                class="upload-demo"
                ref="upload"
                :action="uploadUrl"
                :on-preview="handlePreview"
                :on-remove="handleRemove"
                :file-list="fileList" :data="uploadData" 
                :on-success="uploadSuccess" 
                list-type="picture">
                <el-button size="small" type="primary">点击上传</el-button>
                <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，图片数量限制一张</div>
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <set-owner :show.sync="isShowChoose"></set-owner>
      <!-- <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm('form')">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div> -->
    </el-dialog>
  </div>
</template>

<script>
  import ChooseJobNumber from '../../role-permission-manage/set-owner';
  import { elecSignatureInfo, saveElecSignature } from 'service/task';
  export default {
    components: {
      'set-owner': ChooseJobNumber
    },
    data() {
      return {
        uploadUrl: `${process.env.API_URL}elecSignature/upload`,
        dialogVisible: false,
        limitNum: 0,
        formData: {
          name: '',
          userName: ''
        },
        uploadData: {
          elecSignatureId: this.modifyId
        },
        isShowChoose: false,
        fileList: []
      }
    },
    props: {
      show: {
        type: Boolean,
        default: false,
      },
      modifyId: {
        type: String
      }
    },
    watch: {
      show() {
        this.dialogVisible = this.show;
        this.uploadData.elecSignatureId = this.modifyId;
        if(this.dialogVisible) {
          // this.axios.get('/system/deviceInfo', {
            // id: this.modifyId
          // })
          elecSignatureInfo(this.modifyId)
            .then(res => {
              this.fileList = [];
              this.formData.name = res.data.result.name;
              this.formData.userName = res.data.result.userName;
               let file = {
                name: res.data.result.signPicName,
                url: `${process.env.API_URL}elecSignature/downloadAttach?elecSignatureId=${res.data.result.id}`,
                status: 'success',
                percentage: 100,
              };
              this.fileList.push(file);
            })
        }
      }
    },
    methods: {
      handleClose() { /* 关闭的时候更新状态值 */
        this.dialogVisible = false;
      },
      submitUpload() {
        this.$refs.upload.submit();
      },
      uploadSuccess(res, file, fileList) {
        console.log(res)
        console.log(file);
        if (res.success) {
          for(let i = 0; i < fileList.length; i++) {
            fileList.splice(i, 1);
          }
        }
        //   let file = {
        //         name: res.data.result.signPicName,
        //         url: `${process.env.API_URL}elecSignature/downloadAttach?elecSignatureId=${res.data.result.id}`,
        //         status: 'success',
        //         percentage: 100,
        //   };
        //   fileList.push(file);
        //   this.$message({
        //     message: res.data.message,
        //     type: 'success',
        //     duration: 1000
        //     });
        // } else {
        //   this.$alert(res.message, "提示", {
        //     type: "success"
        //   });
        // }
      },
      handleRemove(file, fileList) {
        postDeleteAttach(file.id).then(res => {
          if (!res.data.success) {
            this.$alert(res.data.message, "提示", {
              type: "error"
            });
          }
        })
        console.log(file, fileList);
      },
      handlePreview(file) {
        alert(fileList.length);
      },
      submitForm() {
        this.$refs.upload.submit();
    }
  }
}
</script>
